﻿using ToolKitIESiteList.Library;
using ToolKitManagerIESiteList.Util;
using System;
using System.Windows.Forms;

namespace ToolKitManagerIESiteList
{
    public partial class FrmAddEditItem : Form
    {
        public FrmAddEditItem()
        {
            InitializeComponent();
            DataSite = new IEMXmlItem();
            DataSite.CompatMode = CompatMode.IE11;
            DataSite.OpenIn = OpenIn.IE11;
            DataSite.Comment = string.Empty;
            CboMode.Items.Clear();
            foreach (var item in Enum.GetNames(typeof(CompatMode)))
            {
                CboMode.Items.Add(item);
            }
            CboOpenIn.Items.Clear();
            foreach (var item in Enum.GetNames(typeof(OpenIn)))
            {
                CboOpenIn.Items.Add(item);
            }
        }
        public bool NewSite { get; set; }

        public IEMXmlItem DataSite { get; private set; }

        private void BntClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void FrmAddEditItem_Load(object sender, EventArgs e)
        {
            if (DataSite.Uri == null)
            {
                TxtUrl.Text = string.Empty;
            }
            else
            {
                TxtUrl.Text = string.Format("{0}{1}", DataSite.Uri.GetHostAndPort(), DataSite.Uri.AbsolutePath);
            }
            TxtComment.Text = DataSite.Comment;
            CboMode.SelectedItem = DataSite.CompatMode.ToString();
            CboOpenIn.SelectedItem = DataSite.OpenIn.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            TxtUrl.Text = TxtUrl.Text.Trim();
            if (TxtUrl.Text.Length == 0)
            {
                this.ShowWarning(Properties.Resources.UrlRequeried, AssemblyHelper.AssemblyTitle);
                return;
            }
            var url = HelperEmie.CreateValidUri(HelperEmie.AddScheme(TxtUrl.Text));
            if (url == null)
            {
                this.ShowWarning(Properties.Resources.UrlNotValid, AssemblyHelper.AssemblyTitle);
                return;
            }
            if (CboMode.SelectedItem == null)
            {
                this.ShowWarning(Properties.Resources.CompatModeRequeried, AssemblyHelper.AssemblyTitle);
                return;
            }
            if (CboOpenIn.SelectedItem == null)
            {
                this.ShowWarning(Properties.Resources.OpenInRequeried, AssemblyHelper.AssemblyTitle);
                return;
            }
            var qtd = EmieDocument.CountUri(url);
            if (qtd > 0 && NewSite)
            {
                this.ShowWarning(Properties.Resources.SiteAlreadyExists, AssemblyHelper.AssemblyTitle);
                return;
            }
            else if (qtd > 1 && !NewSite)
            {

                this.ShowWarning(Properties.Resources.SiteAlreadyExists, AssemblyHelper.AssemblyTitle);
                return;
            }
            else if (qtd == 1 && !NewSite)
            {
                if (EmieDocument.FindIndexUri(string.Format("{0}{1}", url.GetHostAndPort(), url.AbsolutePath)) != EmieDocument.FindIndexUri(string.Format("{0}{1}", DataSite.Uri.GetHostAndPort(), DataSite.Uri.AbsolutePath)))
                {
                    this.ShowWarning(Properties.Resources.SiteAlreadyExists, AssemblyHelper.AssemblyTitle);
                    return;
                }
            }
            if (DataSite.Uri == null)
            {
                ManagerHotTracker.AddTraker(ActionTracker.AddSite, Properties.Resources.AddNewSite,string.Format("{0}{1}", url.GetHostAndPort(), url.AbsolutePath) ,
                    new IEModeDataItem[] {
                        new IEModeDataItem {
                            HostPort = url.GetHostAndPort(),
                            AbsolutePath = url.AbsolutePath,
                            Comment = TxtComment.Text,
                            CompatMode = (CompatMode)Enum.Parse(typeof(CompatMode),CboMode.SelectedItem.ToString()),
                            OriOpenIn = (OpenIn)Enum.Parse(typeof(OpenIn), CboOpenIn.SelectedItem.ToString()),
                            Uri = url
                } });
            }
            else
            {
                ManagerHotTracker.AddTraker(ActionTracker.UpdateSite, Properties.Resources.RemoveSite, "",
                    new IEModeDataItem[] {
                        new IEModeDataItem {
                            HostPort = DataSite.Uri.GetHostAndPort(),
                            AbsolutePath = DataSite.Uri.AbsolutePath,
                            Comment = DataSite.Comment,
                            CompatMode = DataSite.CompatMode,
                            OriOpenIn = DataSite.OpenIn,
                            Uri = DataSite.Uri
                } });
                ManagerHotTracker.AddTraker(ActionTracker.UpdateSite, Properties.Resources.AddNewSite, "",
                    new IEModeDataItem[] {
                        new IEModeDataItem {
                            HostPort = url.GetHostAndPort(),
                            AbsolutePath = url.AbsolutePath,
                            Comment = TxtComment.Text,
                            CompatMode = (CompatMode)Enum.Parse(typeof(CompatMode),CboMode.SelectedItem.ToString()),
                            OriOpenIn = (OpenIn)Enum.Parse(typeof(OpenIn), CboOpenIn.SelectedItem.ToString()),
                            Uri = url
                } });
            }
            EmieDocument.AddOrUpdateDataItem( (DataSite.Uri == null)?"":string.Format("{0}{1}",DataSite.Uri.GetHostAndPort(),DataSite.Uri.AbsolutePath),
                string.Format("{0}{1}",url.GetHostAndPort(),url.AbsolutePath),
                (CompatMode)Enum.Parse(typeof(CompatMode), CboMode.SelectedItem.ToString()),
                (OpenIn)Enum.Parse(typeof(OpenIn), CboOpenIn.SelectedItem.ToString()),
                TxtComment.Text);
            this.DialogResult = DialogResult.OK;
        }

        private void TxtUrl_Leave(object sender, EventArgs e)
        {
            var xmlUrl = TxtUrl.Text.Trim();
            if (string.IsNullOrEmpty(xmlUrl)) return;
            if (Properties.Settings.Default.RemoveP80)
            {
                var aux = new Uri(HelperEmie.AddScheme(xmlUrl));
                xmlUrl = string.Format("{0}{1}", aux.GetHostAndPort().Replace(":80", ""), aux.AbsolutePath);
            }
            var newtext = HelperEmie.CreateValidUri(xmlUrl);
            if (newtext != null)
            {
                var url = newtext.GetHostAndPort() + newtext.AbsolutePath;
                TxtUrl.Text = url;
            }
        }

        private void TxtUrl_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
